//
//  GroupingBookViewController.m
//  lsOA
//
//  Created by 蓝水软件 on 16/9/14.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//

#import "GroupingBookViewController.h"
#import "MyInformationTableViewCell.h"
#import "NSMutableArray+FilterElement.h"
#import "AddressBookViewController.h"
#import "FollwTableViewCell.h"

@interface GroupingBookViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *groundArray;    //部门分组后数据
    NSArray *array;                 //原始数据 未分组
    UITableView *table;
    NSMutableArray *headArray;      //组标题数据
    NSMutableArray *isShow; //控制每组是否显示
    
}

@end

@implementation GroupingBookViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    array = [[NSUserDefaults standardUserDefaults]objectForKey:@"ContactsBook"];
    groundArray = [NSMutableArray new];
    headArray = [NSMutableArray new];
    for (NSDictionary *dic in array) {
        [headArray addObject:dic[@"DepName"]];
    }
    NSLog(@"%@",headArray);
    [headArray filterTheSameElement];
    isShow = [NSMutableArray new];
    for (int i = 0;  i < headArray.count; i++) {
        NSMutableArray *tempArray = [NSMutableArray new];
        [isShow addObject:@"0"];
        int forinTag = 0;
        for (NSDictionary *dic in array) {
            forinTag++;
            if ([dic[@"DepName"] isEqualToString:headArray[i]]) {
                [tempArray addObject:dic];
            }
            if (forinTag == array.count) {
                [groundArray addObject:tempArray];
            }
        }
    }

    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSWidth, KSHeight-128) style:UITableViewStylePlain];
    [self.view addSubview:table];
    table.delegate = self;
    table.dataSource = self;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{

    return headArray.count;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([isShow[section] isEqualToString:@"0"]) {
        return 0;
    }else{
        NSArray *array11  = groundArray[section];
        return array11.count;
    }


}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSWidth, 45)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    NSArray *tempArray = groundArray[section];
    titleLabel.text = [NSString stringWithFormat:@"%@",headArray[section]];
    titleLabel.textColor = [UIColor blackColor];
    
    UILabel *numLabel = [UILabel new];
    numLabel.textAlignment = NSTextAlignmentRight;
    numLabel.textColor = [UIColor lightGrayColor];
    numLabel.font = [UIFont systemFontOfSize:13*LRLRadioWidth];
    numLabel.text = [NSString stringWithFormat:@"%ld",tempArray.count];
    [view addSubview:numLabel];
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(100);
        make.right.mas_equalTo(-10);
        make.centerY.equalTo(view);
    }];
    

    [view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view);
        make.left.mas_equalTo(45);
        make.width.mas_equalTo(KSWidth);
    }];
    
    UIButton *showButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:showButton];
    [showButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view);
        make.left.mas_equalTo(10);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(20);
    }];
    //更具当前是否展开设置图片
    if ([isShow[section] intValue] == 1) {
        [showButton setImage:[UIImage imageNamed:@"已展开"] forState:UIControlStateNormal];
    }else{
        [showButton setImage:[UIImage imageNamed:@"未展开"] forState:UIControlStateNormal];
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    [view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(view);
        make.width.mas_equalTo(KSWidth);
        make.height.equalTo(view.mas_height);
    }];
    button.tag = 4000+section;
    [button addTarget:self action:@selector(btnOpenList:) forControlEvents:UIControlEventTouchUpInside];

    
    UIView *lineView = [UIView new];
    [view addSubview:lineView];
    lineView.backgroundColor = [UIColor colorWithWhite:0.800 alpha:1.000];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(view);
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.height.mas_equalTo(1);
    }];
    return view;
}

- (void)btnOpenList:(UIButton *)sender{
    NSMutableArray *tempArray = [NSMutableArray new];
    for (int i = 0; i < isShow.count; i++) {
        if (i == sender.tag-4000) {
            NSString *value = isShow[i];
            value = [value isEqualToString:@"0"] ? @"1":@"0";
            [tempArray addObject:value];
            
            
        }else{
            [tempArray addObject:isShow[i]];
        }
    }
    isShow = [tempArray copy];
    [table reloadData];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AddressBookViewController *addressBook = [[AddressBookViewController alloc]init];
    addressBook.dic = groundArray[indexPath.section][indexPath.row];
    addressBook.type = @"person";    //type : person为个人  company为公司
    addressBook.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:addressBook animated:YES];


}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    static NSString *CellID = @"CellID";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
//    if (!cell) {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellID];
//    }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//
//    NSDictionary *dic =  groundArray[indexPath.section][indexPath.row];
//    cell.textLabel.text = dic[@"RealName"];
//    cell.textLabel.textColor = [UIColor blueColor];
//    return cell;
    
    
    static NSString *cellID = @"FollwTableViewCell";
    FollwTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [FollwTableViewCell viewFromBundle];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSDictionary *dic =  groundArray[indexPath.section][indexPath.row];
    cell.nickNmaeLabel.text = [dic objectForKey:@"RealName"];
    if ([[dic objectForKey:@"sexName"] isEqualToString:@"男"]) {
        cell.headImageView.image = [UIImage imageNamed:@"头像(男)"];
    }else{
        cell.headImageView.image = [UIImage imageNamed:@"头像(女)"];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
    
}


@end
